package com.xiaoxie.web.quartz;

import com.github.pagehelper.PageInfo;
import com.xiaoxie.common.union.R;
import com.xiaoxie.quartz.domain.dto.UniqJobDto;
import com.xiaoxie.quartz.domain.dto.GroupDto;
import com.xiaoxie.quartz.domain.JobEntity;
import com.xiaoxie.quartz.domain.dto.JobDto;
import com.xiaoxie.quartz.domain.dto.QueryJobDto;
import com.xiaoxie.quartz.service.QuartzService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job")
@Tag(name = "定时任务管理",description = "定时任务管理")
public class JobController {
    @Resource
    private QuartzService quartzService;


    @Operation(summary = "获取所有分组")
    @GetMapping("/AllGroups")
    public R getAllGroups() {
        List<GroupDto> groups = quartzService.getAllGroups();
        return R.ok(groups);
    }

    @Operation(summary = "获取分组下的所有Job")
    @GetMapping("/getJobsByGroup")
    public R getJobsByGroup(@RequestParam("jobGroup") String jobGroup) {
        List<JobDto> jobs = quartzService.getJobsByGroup(jobGroup);
        return R.ok(jobs);
    }

    @Operation(summary = "根据jobGroup和jobName判断任务是否存在")
    @GetMapping("/checkJobGroupAndJobName")
    public R checkJobGroupAndJobName(@RequestParam("jobGroup") String jobGroup,
                                     @RequestParam("jobName") String jobName) {
        boolean result = quartzService.checkJobGroupAndJobName(jobGroup, jobName);
        return R.ok(result);
    }

    @Operation(summary = "根据jobClassName判断是否符合要求")
    @GetMapping("/checkJobClassName")
    public R checkJobClassName(@RequestParam("jobClass") String jobClass) {
        boolean result = quartzService.checkJobClassName(jobClass);
        return R.ok(result);
    }

    @Operation(summary = "根据提供的corn表达式判断是否符合要求")
    @GetMapping("/checkCronExpression")
    public R checkCronExpression(@RequestParam("cronExpression") String cronExpression) {
        boolean result = quartzService.checkCronExpression(cronExpression);
        return R.ok(result);
    }

    @Operation(summary = "分页查询Job")
    @GetMapping("/list")
    public R getJobList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,QueryJobDto queryJobDto) {

        PageInfo<JobEntity> pageInfo =  quartzService.getJobList(pageNum, queryJobDto);
        return R.ok(pageInfo);
    }

    @PostMapping("/add")
    @Operation(summary = "添加定时任务")
    public R addJob(@RequestBody JobEntity job) {
        if (job == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.addJob(job.getJobClass(),
                    job.getJobName(),
                    job.getJobGroup(),
                    job.getCronExpression(),
                    job.getDescription(),
                    job.getInitStatus());
            return R.ok("添加定时任务成功！");
        } catch (SchedulerException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    @Operation(summary = "更新定时任务")
    @PutMapping ("/update")
    public R updateJob(@RequestBody JobEntity job) {
        if (job == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.updateJob(job.getJobClass(),
                    job.getJobName(),
                    job.getJobGroup(),
                    job.getCronExpression(),
                    job.getDescription(),
                    job.getInitStatus());
            return R.ok("更新定时任务成功！");
        } catch (SchedulerException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "批量删除")
    @PostMapping("/batchDelete")
    public R batchDeleteJob(@RequestBody List<UniqJobDto> deleteJobsDtos) {
        for (UniqJobDto deleteJobsDto : deleteJobsDtos) {
            try {
                quartzService.deleteJob(deleteJobsDto.getJobName(), deleteJobsDto.getJobGroup());
            } catch (SchedulerException e) {
                throw new RuntimeException(e);
            }
        }
        return R.ok("删除任务成功！");
    }

    @Operation(summary = "触发定时任务")
    @PostMapping("/trigger")
    public R triggerJob(@RequestBody UniqJobDto jobDto) {
        if (jobDto == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.triggerJob(jobDto.getJobName(), jobDto.getJobGroup());
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        return R.ok("触发任务成功！");
    }

    @Operation(summary = "暂停定时任务")
    @PostMapping("/pause")
    public R pauseJob(@RequestBody JobEntity job) {
        if (job == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.pauseJob(job.getJobName(), job.getJobGroup());
            return R.ok("暂停定时任务成功！");
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "恢复定时任务")
    @PostMapping("/resume")
    public R resumeJob(@RequestBody JobEntity job) {
        if (job == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.resumeJob(job.getJobName(), job.getJobGroup());
            return R.ok("恢复定时任务成功！");
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "删除定时任务")
    @DeleteMapping("/delete")
    public R deleteJob(@RequestParam("jobGroup") String jobGroup,
                       @RequestParam("jobName") String jobName) {
        if (jobGroup == null || jobName == null) {
            return R.fail("参数不能为空！");
        }
        try {
            quartzService.deleteJob(jobName, jobGroup);
            return R.ok("删除定时任务成功！");
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "根据groupName和jobName获取定时任务")
    @GetMapping("/getByGroupNameAndJobName")
    public R getByGroupNameAndJobName(@RequestParam("jobGroup") String jobGroup,
                                      @RequestParam("jobName") String jobName) {
        if (jobGroup == null || jobName == null) {
            return R.fail("参数不能为空！");
        }
        JobEntity job = quartzService.getByGroupNameAndJobName(jobGroup, jobName);
        return R.ok(job);
    }
}
